package org.jiuling.aliyun.sms.convert;

import org.jiuling.aliyun.sms.exception.AliYunSMSRuntimeException;
import org.jiuling.aliyun.sms.model.AddShortUrlResponseModel;
import org.jiuling.basics.converter.DateConvert;
import org.jiuling.basics.enums.DateTimeFormatEnum;
import org.jiuling.basics.exception.BasicsException;
import org.jiuling.basics.validation.ObjectVerify;
import org.jiuling.basics.validation.StringVerify;

/**
 * 添加短链接响应转换
 * @author 紫灵
 * @date 2023年02月21日 15时51分40秒
 */
public class AddShortUrlResponseConvert {

    /** 私有构造方法，禁止创建对象 */
    private AddShortUrlResponseConvert() {}

    public static AddShortUrlResponseModel bodyToModel(com.aliyun.sdk.service.dysmsapi20170525.models.AddShortUrlResponseBody addShortUrlResponseBody){
        AddShortUrlResponseModel addShortUrlResponseModel = new AddShortUrlResponseModel();
        addShortUrlResponseModel.setCode(addShortUrlResponseBody.getCode());
        addShortUrlResponseModel.setMessage(addShortUrlResponseBody.getMessage());
        addShortUrlResponseModel.setRequestId(addShortUrlResponseBody.getRequestId());

        com.aliyun.sdk.service.dysmsapi20170525.models.AddShortUrlResponseBody.Data dataBody = addShortUrlResponseBody.getData();
        if(!ObjectVerify.isNull(dataBody)) {
            AddShortUrlResponseModel.Data data = new AddShortUrlResponseModel.Data();
            data.setShortUrl(dataBody.getShortUrl());
            data.setSourceUrl(dataBody.getSourceUrl());
            String expireDate = dataBody.getExpireDate();
            if(!StringVerify.isBlank(expireDate)) {
                try {
                    data.setExpireDate(DateConvert.stringToDate(expireDate, DateTimeFormatEnum.yyyy_MM_dd_HH_mm_ss));
                } catch (BasicsException e) {
                    throw new AliYunSMSRuntimeException(e.getLocalizedMessage());
                }
            }
            addShortUrlResponseModel.setData(data);
        }

        return addShortUrlResponseModel;
    }

    public static AddShortUrlResponseModel bodyToModel(com.aliyun.dysmsapi20170525.models.AddShortUrlResponseBody addShortUrlResponseBody){
        AddShortUrlResponseModel addShortUrlResponseModel = new AddShortUrlResponseModel();
        addShortUrlResponseModel.setCode(addShortUrlResponseBody.getCode());
        addShortUrlResponseModel.setMessage(addShortUrlResponseBody.getMessage());
        addShortUrlResponseModel.setRequestId(addShortUrlResponseBody.getRequestId());

        com.aliyun.dysmsapi20170525.models.AddShortUrlResponseBody.AddShortUrlResponseBodyData dataBody = addShortUrlResponseBody.getData();
        if(!ObjectVerify.isNull(dataBody)) {
            AddShortUrlResponseModel.Data data = new AddShortUrlResponseModel.Data();
            data.setShortUrl(dataBody.getShortUrl());
            data.setSourceUrl(dataBody.getSourceUrl());
            String expireDate = dataBody.getExpireDate();
            if(!StringVerify.isBlank(expireDate)) {
                try {
                    data.setExpireDate(DateConvert.stringToDate(expireDate, DateTimeFormatEnum.yyyy_MM_dd_HH_mm_ss));
                } catch (BasicsException e) {
                    throw new AliYunSMSRuntimeException(e.getLocalizedMessage());
                }
            }
            addShortUrlResponseModel.setData(data);
        }

        return addShortUrlResponseModel;
    }
}
